package com.example.administrator.netdemo.base;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cyw.baselibrary.R;
import com.cyw.baselibrary.utils.ToastUtils;
import com.cyw.baselibrary.view.varyview.LoadViewHelper;
import com.cyw.baselibrary.view.varyview.VaryViewHelper;
import com.example.administrator.netdemo.network.RequestModelManager;


public abstract class BaseFragment extends Fragment {

    protected Context context;
    protected Activity activity;
    protected View rootView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity().getApplicationContext();
        activity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), null);
            initLoadingHelper();
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        initView();
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        cancelRequest();
    }

    protected void cancelRequest() {
        RequestModelManager.getInstance().cancelAllRequest(this.getClass().getName());
    }

    public abstract int getLayoutId();

    public abstract void initView();

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void showToast(String content) {
        ToastUtils.showShort(context, content);
    }

    public void showToastLong(String content) {
        ToastUtils.showLong(context, content);
    }

    public <T extends View> T findView(int viewId) {
        return (T) rootView.findViewById(viewId);
    }


    //--------------------------------以下是界面加载助手相关---------------------------------
    /**
     * 界面加载助手
     */
    private LoadViewHelper loadViewHelper;


    /**
     * 初始化界面加载助手
     */
    public void initLoadingHelper() {
        View content = findView(R.id.view_content);
        if (loadViewHelper == null && content != null) {
            loadViewHelper = new LoadViewHelper(new VaryViewHelper(content));
        }
    }


    /**
     * 显示请求数据为空的界面
     */
    public void showEmptyView(View.OnClickListener listener, String message, String buttonText) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmpty(listener, message, buttonText, R.mipmap.load_empty);
    }

    /**
     * 显示出错的界面
     */
    public void showErrorView(String message) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(message, R.mipmap.icon_load_error);
    }

    /**
     * 显示出错的界面
     */
    public void showErrorView(View.OnClickListener listener, String message, String buttonText) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showError(listener, message, buttonText, R.mipmap.icon_load_error);
    }

    /**
     * 显示请求数据为空的界面
     */
    public void showEmptyView(View.OnClickListener listener, String message, String buttonText, int imageId) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmpty(listener, message, buttonText, imageId);
    }

    /**
     * 显示请求数据为空的界面(没有按钮)
     */
    public void showEmptyViewNoButton(String message) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmptyNoButton(message, R.mipmap.load_empty);
    }

    /**
     * 显示请求数据为空的界面(没有按钮)
     */
    public void showEmptyViewNoButton(String message, int imageId) {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showEmptyNoButton(message, imageId);
    }


    /**
     * 显示加载中图标
     */
    public void showLoadingView() {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.showLoading();
    }

    /**
     * 显示加载成功的界面(即展示数据的界面)
     */
    public void showSuccessView() {
        if (loadViewHelper == null) {
            return;
        }
        loadViewHelper.restore();
    }

}